package com.by.springboottwodatabases.config;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import javax.sql.DataSource;

/**
 * 配置两个数据库的数据源
 */
@Configuration
public class DataSourceConfig {

    //主数据源 one数据源
    @Primary
    @Bean(name = "oneDataSource")
    @Qualifier("oneDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.one")
    public DataSource oneDataSource() {
        return DataSourceBuilder.create().build();
    }

    //two数据源
    @Bean("twoDataSource")
    @Qualifier("oneDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.two")
    public DataSource ds2DataSource() {
        return DataSourceBuilder.create().build();
    }

}
